/*
 * 铁路道岔故障检测信息管理系统
 * Design by 魏梓晟
 * Version :1.0
 */
package com.railway.switchs.service.impl.business;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.railway.switchs.dao.business.SwitchInfoDao;
import com.railway.switchs.domain.dto.business.SwitchInfo;
import com.railway.switchs.domain.query.business.SwitchInfoQuery;
import com.railway.switchs.service.business.ISwitchInfoService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * switch_info - 道岔信息表 Service实现类
 * @author 魏梓晟
 */
@Service("switchInfoService")
public class SwitchInfoServiceImpl implements ISwitchInfoService {

    
    @Autowired
    private SwitchInfoDao switchInfoDao;

	private final int QUERY_LIMLIT = 10000;

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public Long add(SwitchInfo record) {
		record.setCreateTime(new Date());
		record.setVersionNum(1L);
        if(switchInfoDao.add(record)==1){
			return record.getId();
		}
		return 0L;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean edit(SwitchInfo record) {
		record.setModifyTime(new Date());
		record.setVersionNum(switchInfoDao.get(record.getId()).getVersionNum());
        return switchInfoDao.update(record)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public boolean delete(Long id) {

        return switchInfoDao.delete(id)==1;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public SwitchInfo get(Long id) {

        return switchInfoDao.get(id);
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public SwitchInfoQuery query(SwitchInfoQuery query) {
        int count =  switchInfoDao.count(query);
        query.setCount(count);
        query.setData(switchInfoDao.query(query));
        return query;
    }

    @Override
	@Transactional(propagation= Propagation.REQUIRED,timeout=30,rollbackFor={RuntimeException.class, Exception.class})
    public List<SwitchInfo> getList(SwitchInfoQuery query) {
        int count = switchInfoDao.count(query);
        int pags = (count - 1) / QUERY_LIMLIT + 1;
        List<SwitchInfo> switchInfoList = new ArrayList<>(count);
        for(int i = 1 ;i <= pags ; i++ ){
            query.setPage(i);
            query.setLimit(QUERY_LIMLIT);
            switchInfoList.addAll(switchInfoDao.query(query));
        }
        return switchInfoList;
    }

    @Override
    public boolean editRunType(SwitchInfo record) {
        return switchInfoDao.updateRunType(record) > 0;
    }
}
